/**
 * 给你两个单链表的头节点 headA 和 headB,
 * 请你找出并返回两个单链表相交的起始节点,
 * 如果两个链表没有交点，返回 null.
 */
class ListNode {
  constructor(val, next) {
    this.val = val;
    this.next = next;
  }
}
//获取链表长度
function getListLen(head) {
  let len = 0, cur = head;
  while (cur) {
    len++;
    cur = cur.next;
  }
  return len;
}
//获取相交的结点
function getInterSectionNode(headA, headB) {
  let curA = headA, curB = headB, lenA = getListLen(headA), lenB = getListLen(headB);
  // 若A的长度小于B的长度,交换数据
  if (lenA < lenB) {
    [curA, curB] = [curB, curA];
    [lenA, lenB] = [lenB, lenA];
  }
  let i = lenA - lenB;
  while (i-- > 0) {
    curA = curA.next;
  }
  while (curA && curA !== curB) {
    curA = curA.next;
    curB = curB.next;
  }
  return curA;
}